﻿package  com.snzflash.control.scroll  
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.geom.Rectangle;
	/**
	 * Базовый класс для скроллера/прогресс лайна в плеера
	 * 
	 * Используется как базовый класс для мувиклипа в swc библиотеки
	 * @author firsoff maxim, tel: +79222345364, skype: maxim.firsoff
	 */
	public class ScrollBase extends Sprite
	{
		
		// PROPERTIES
	
		// public
		// иконка за которую перетаскивают
		public var icon:MovieClip;
		// линия проигрывания
		public var played:MovieClip;
		// линия загрузки
		public var loaded:MovieClip;
		// фон
		public var back:MovieClip;
		
		
		// protected
		// прогресс проигрывания
		protected var _playProgress:Number = 0;
		// прогресс загрузки
		protected var _loadProgress:Number = 0;
		// факт нажатия
		protected var _pressed:Boolean;
		
		// == STATIC==
		
		
		// CONSTRUCTOR
		public function ScrollBase() 
		{
			init();
		
		};
		
		// METHODS
	
		// public
		
		public function progressColor(color:uint):void 
		{
			toColor(played, color);
		}
		
		public function backgroundColor(color:uint):void 
		{
			toColor(back, color);
		}
		
		public function loadingColor(color:uint):void 
		{
			toColor(loaded, color);
		}
		
		public function thumbColor(color:uint):void 
		{
			toColor(icon, color);
		}
		/**
		 * Уничтожение
		 */
		public function destroy():void 
		{
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove);
			stage.removeEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
			icon.removeEventListener(MouseEvent.MOUSE_DOWN, onIconDown);
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			stage.removeEventListener(Event.MOUSE_LEAVE, onMouseLeaveFromStage);
		};
		
		// protected
		protected function init():void
		{
			icon.buttonMode = icon.useHandCursor = true;
			
			
			playProgress = _playProgress;
			loadProgress = _loadProgress;
			
			setEvents()
		};
		
		protected function toColor(target:DisplayObject, color:uint):void 
		{
			var ct:ColorTransform = new ColorTransform();
			ct.color = color;
			target.transform.colorTransform = ct;
		}
		
		protected function setEvents():void 
		{
			icon.addEventListener(MouseEvent.MOUSE_DOWN, onIconDown);
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}
		
		protected function dispatchChange():void 
		{
			//dispatchEvent(new NavigationEvent(NavigationEvent.SEEK, false, false, _playProgress));
			dispatchEvent(new Event(Event.CHANGE));
		};
		
		
		protected function setPlayLineProgress():void
		{
			//trace(played, back)
			played.width = back.width * _playProgress;
		};
		// getters/setters
		/**
		 * прогресс проигрывания
		 */
		public function get playProgress():Number { return _playProgress; };
		
		public function set playProgress(value:Number):void 
		{
			//if(value == NaN) return
			if (_pressed) return;
			_playProgress = value;
			setPlayLineProgress();
			icon.x  = _playProgress *  (back.width -icon.width);
			//// trace(_playProgress)
			//playProgress =  ( icon.x )  / (back.width -icon.width);
		};
		
		/**
		 * прогресс загрузки
		 */
		public function get loadProgress():Number { return _loadProgress; };
		
		public function set loadProgress(value:Number):void 
		{
			_loadProgress = value;
			loaded.width = back.width * value;
			//trace(value);
		};
		
		// == STATIC==
		
		
		// HANDLERS
		protected function onAddedToStage(event:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			stage.addEventListener(Event.MOUSE_LEAVE, onMouseLeaveFromStage);
		};
		
		
		protected function onIconDown(event:MouseEvent):void 
		{
			
			_pressed = true;
			// определяем прямоугольник для перетаскивания
			var drag_width:Number = loaded.width - icon.width;
			//trace(loaded.height);
			if (drag_width <= 0) return; 
			var drag_rect:Rectangle = new Rectangle(0, loaded.y, drag_width, 0);
			icon.startDrag(false, drag_rect);
			// чтобы бы плавненько
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove);
			stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
		};
		
		protected function onStageMouseUp(event:MouseEvent = null):void 
		{
			_pressed = false;
			icon.stopDrag();
			playProgress =  ( icon.x )  / (back.width -icon.width);
			if (stage)
			{
				stage.removeEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove);
				stage.removeEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
			}
			
			dispatchChange();
		};
		
		protected function onStageMouseMove(event:MouseEvent):void 
		{
			_playProgress =  ( icon.x )  / (back.width -icon.width);
			setPlayLineProgress();
			event.updateAfterEvent();
		};
		
		protected function onMouseLeaveFromStage(event:Event):void 
		{
			//_pressed = false;
			//icon.stopDrag();
			onStageMouseUp(); // ???
			stage.removeEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
		};
	};

};